# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
from proxycheck import checkProxy
import datetime
import json
import codecs
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

class ProxyPipeline(object):
    def __init__(self):
        self.fileWriterMap = {}


    def process_item(self, item, spider):
        if not spider.name in self.fileWriterMap:
            filename = spider.name + str(datetime.date.today()) + '.txt'
            self.fileWriterMap[spider.name] = codecs.open(filename, 'wb', encoding='utf-8')
        print item['ip']
        if checkProxy(item['ip'], item['port']):
            line = "%s %s %s %s\n" %(item['ip'], item['port'], item['loc'], item['ptype'])
            #json.dumps(dict(item))+'\n'
            #self.file.write(line.decode("unicode_escape"))
            self.fileWriterMap[spider.name].write(line)
            return item
        else:
            print "%s:%s not reachable"%(item['ip'],item['port'])